package com.smart.mac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.mac.common.R;
import com.smart.mac.model.dto.FlowQueryDTO;
import com.smart.mac.model.dto.FlowTaskDTO;
import com.smart.mac.model.vo.FlowProcInstVO;
import com.smart.mac.model.vo.FlowTaskVO;
import com.smart.mac.model.vo.UserTaskSimpleVO;
import com.smart.mac.service.FlowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 任务
 *
 * @Author mac
 */
@RestController
@RequestMapping("/flowable/task")
public class FlowTaskController {

    @Autowired
    private FlowTaskService flowTaskService;



    /**
     *  任务撤回（只有未签收或审核的可以撤回）
     *  场景：审批任审批后，后悔了或别的原因，需要回退任务
     * @param: [instanceId 流程实例ID,taskDefKey 已经处理任务key]
     * @return: com.elephant.demo.common.R
     * @author mac
     */
    @PostMapping(value = "/backTask/{instanceId}/{taskDefKey}")
    public R backTask(@PathVariable String instanceId,@PathVariable String taskId) {
        flowTaskService.backTask(instanceId,taskId);
        return R.ok("任务回退成功");
    }


   /**
   * 我发起的流程
   * @param: [queryDTO]
   * @return: com.elephant.demo.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.elephant.demo.model.vo.FlowProcInstVO>>
   * @author mac
   */
    @PostMapping(value = "/myProcess")
    public  R<Page<FlowProcInstVO>> myProcess(@RequestBody FlowQueryDTO queryDTO) {
        return R.ok(flowTaskService.myProcess(queryDTO));
    }


    /**
    * 获取待办列表
    * @param: [queryDTO]
    * @return: com.elephant.demo.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.elephant.demo.model.vo.FlowTaskVO>>
    * @author mac
    */
    @PostMapping(value = "/todoList")
    public R<Page<FlowTaskVO>> todoList(@RequestBody FlowQueryDTO queryDTO) {
        return R.ok(flowTaskService.todoList(queryDTO));
    }

    /**
    * 获取已办任务
    * @param: [queryDTO]
    * @return: com.elephant.demo.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.elephant.demo.model.vo.FlowTaskVO>>
    * @author mac
    */
    @PostMapping(value = "/finishedList")
    public  R<Page<FlowTaskVO>> finishedList(@RequestBody FlowQueryDTO queryDTO) {
        return R.ok(flowTaskService.finishedList(queryDTO));
    }

    /**
    * 流程历史流转记录
    * @param: [procInsId]
    * @return: com.elephant.demo.common.R<java.util.Map>
    * @author mac
    */
    @GetMapping(value = "/flowRecord")
    public R<Map<String,Object>> flowRecord(String procInsId) {
        return R.ok(flowTaskService.flowRecord(procInsId));
    }


    /**
    * 审批任务
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/complete")
    public R complete(@RequestBody FlowTaskDTO taskDTO) {
        return R.ok(flowTaskService.complete(taskDTO));
    }

   /**
   * 委派任务
   * @param: [taskDTO]
   * @return: com.elephant.demo.common.R
   * @author mac
   */
    @PostMapping(value = "/delegateTask")
    public R delegate(@RequestBody FlowTaskDTO taskDTO) {
        flowTaskService.delegateTask(taskDTO);
        return R.ok("委派成功");
    }

    /**
    * 转办任务
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/assignTask")
    public R assign(@RequestBody  FlowTaskDTO taskDTO) {
        flowTaskService.assignTask(taskDTO);
        return R.ok("转办任务成功");
    }

    /**
    * 签收任务
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/claim")
    public R claim(@RequestBody  FlowTaskDTO taskDTO) {
        flowTaskService.claim(taskDTO);
        return R.ok("签收任务成功");
    }

   /**
   * 取消签收任务
   * @param: [taskDTO]
   * @return: com.elephant.demo.common.R
   * @author mac
   */
    @PostMapping(value = "/unClaim")
    public R unClaim(@RequestBody  FlowTaskDTO taskDTO) {
        flowTaskService.unClaim(taskDTO);
        return R.ok("取消签收任务成功");
    }

    /**
    * 加签
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/addSign")
    public R addSign(@RequestBody FlowTaskDTO taskDTO) {
        flowTaskService.addSign(taskDTO);
        return R.ok("加签成功");
    }

    /**
    * 减签
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/deleteSign")
    public R deleteSign(@RequestBody FlowTaskDTO taskDTO) {
        flowTaskService.deleteSign(taskDTO);
        return R.ok("减签成功");
    }

    /**
    * 实时流程图
    * @param: [response, procInstId 流程实例id]
    * @return: void
    * @author mac
    */
    @GetMapping("/diagram/{procInstId}")
    public void genProcessDiagram(HttpServletResponse response,
                                  @PathVariable("procInstId") String procInstId) {
        InputStream inputStream = flowTaskService.diagram(procInstId);
        OutputStream os = null;
        BufferedImage image = null;
        try {
            image = ImageIO.read(inputStream);
            response.setContentType("image/png");
            os = response.getOutputStream();
            if (image != null) {
                ImageIO.write(image, "png", os);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
    * 驳回任务
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/reject")
    public R taskReturn(@RequestBody FlowTaskDTO taskDTO) {
        flowTaskService.taskReject(taskDTO);
        return R.ok("驳回成功");
    }

    /**
    * 获取所有可回退的节点
    * @param: [taskDTO]
    * @return: com.elephant.demo.common.R
    * @author mac
    */
    @PostMapping(value = "/returnList")
    public R<List<UserTaskSimpleVO>> findReturnTaskList(@RequestBody FlowTaskDTO taskDTO) {
        return R.ok(flowTaskService.findReturnTaskList(taskDTO));
    }






}
